package com.haruhinav.service.impl;

import com.haruhinav.Utils.TimeTool;
import com.haruhinav.entity.Note;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.mapper.NoteMapper;
import com.haruhinav.service.NoteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class NoteServiceImpl implements NoteService {

    @Resource
    private NoteMapper noteMapper;
    @Resource
    private TimeTool timeTool;
    //    根据用户id查询
    @Override
    public ResultVO findNoteByUId(int userId, boolean noContent,int tokenUId) {
        ResultVO result = new ResultVO();
        if(userId!=tokenUId){
            result.setCode(-1);
            result.setMsg("非正常获取！");
            result.setType("error");
            return result;
        }

        List<Note> notes = null;
        if(noContent){
//            不需要查询内容
            notes = noteMapper.findNoteByUIdNoContent(userId);
        }else{
//        查询完整
            notes = noteMapper.findNoteByUId(userId);
        }
        notes.forEach(n->{
//            if(n.getLastUpdateTime()!=null && n.getLastUpdateTime()!="NULL"){
//
//            }
            n.setLastUpdateTime(timeTool.SubTime(n.getLastUpdateTime()));
            n.setCreateTime(timeTool.SubTime(n.getCreateTime()));
        });
//        result.setCode(0);
//        result.setMsg("OK");
        result.setData(notes);
        return result;
    }
    //    根据id查询
    @Override
    public ResultVO findOneById(Note note, int tokenUId) {
        ResultVO result = new ResultVO();
        Note resultNote = null;
        if(note!=null && note.getUserId()==tokenUId){
            result.setCode(0);
            result.setMsg("OK");
            resultNote = noteMapper.findOneById(note.getId());
//            if(resultNote.getLastUpdateTime() != null && resultNote.getLastUpdateTime()!="NULL"){
//
//            }
            resultNote.setLastUpdateTime(timeTool.SubTime(resultNote.getLastUpdateTime()));
            resultNote.setCreateTime(timeTool.SubTime(resultNote.getCreateTime()));
            result.setData(resultNote);
            return result;
        }
        result.setCode(-1);
        result.setMsg("非正常获取！");
        result.setType("error");
        return result;
    }

    @Override
    public ResultVO updateNoteById(Note note, int tokenUId) {
        ResultVO result = new ResultVO();
        if(tokenUId==0 || note.getUserId()!=tokenUId){
            result.setCode(-1);
            result.setMsg("非法修改！");
            result.setType("error");
            return result;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = df.format(new Date());
        note.setLastUpdateTime(datetime);
//        try {
//
//        }catch (Exception e){
//
//        }
        if(noteMapper.updateNoteById(note)>0){
            result.setCode(0);
            result.setMsg("修改成功！");
            result.setData(note);
        }else{
            result.setCode(-2);
            result.setMsg("修改失败！");
            result.setType("error");
        }
        return result;
    }
    //    删除
    @Override
    public ResultVO delNoteById(Note note, int tokenUId) {
        ResultVO result = new ResultVO();
        if(tokenUId==0 || note.getUserId()!=tokenUId){
            result.setCode(-1);
            result.setMsg("非法删除！");
            result.setType("error");
            return result;
        }
        if(noteMapper.delNoteById(note.getId())>0){
            result.setCode(0);
            result.setMsg("删除成功！");
            result.setData(note);
        }else{
            result.setCode(-2);
            result.setMsg("删除失败！");
            result.setType("error");
        }
        return result;
    }

    @Override
    public ResultVO addNote(Note note) {
        ResultVO result = new ResultVO();
        int count = noteMapper.insertNote(note);
        if(count > 0 && note.getId() != 0){
            result.setCode(0);
            result.setMsg("新建成功！");
            result.setData(note);
            return result;
        }
        result.setCode(-1);
        result.setMsg("新建失败！");
        result.setType("error");
        return result;
    }


}
